/*
 * ENUM Discoverer. ENUM Demonstration software, demonstrating the potential of ENUM.
 * 
 * Copyright (C) 2009 SIDN and ISOC.nl
 * 
 * This file is part of ENUM Discoverer.
 *
 * ENUM Discoverer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ENUM Discoverer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ENUM Discoverer.  If not, see <http://www.gnu.org/licenses/>.
 */

package nl.sidn.enumdiscoverer.ui.location;

import nl.sidn.enumdiscoverer.LogConfig;
import nl.sidn.enumdiscoverer.R;
import nl.sidn.enumdiscoverer.ui.incall.LocationViewerConstants;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

/**
 * Reads a location passed in through an Intent and displays that location
 * on a map.
 * @author	Grant Patterson (grant.patterson@catalyst.net.nz)
 */
public class LocationViewer extends MapActivity {
	
	static final String TAG = LogConfig.getLogTag(LocationViewer.class);
	// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
	static final boolean DEBUG = LogConfig.isDebug(TAG);
	// whether VERBOSE level logging is enabled
	static final boolean VERBOSE = LogConfig.VERBOSE;
	
	
	private static final int DEFAULT_LATITUDE = 0;
	private static final int DEFAULT_LONGITUDE = 0;
	private static final int DEFAULT_ZOOM = 16;
	
	private MapView mMapView;
	private BasicItemizedOverlay mItemizedOverlay;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		if (VERBOSE) Log.v(TAG, "onCreate()");
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.location_viewer);

		MapView mapView = (MapView) findViewById(R.id.mapview);
		mMapView = mapView;
		mapView.setBuiltInZoomControls(true);

		// create the overlay and add it to the map view
		Drawable marker = this.getResources().getDrawable(R.drawable.map_marker);
		mItemizedOverlay = new BasicItemizedOverlay(marker);
		mapView.getOverlays().add(mItemizedOverlay);
		
	}
	
	@Override
	protected void onStart() {
		if (VERBOSE) Log.v(TAG, "onStart() with intent " + getIntent());
		super.onStart();
		
		setMapFromIntent(getIntent());
	}
	
	@Override
	public void onNewIntent(Intent newIntent) {
		if (VERBOSE) Log.v(TAG, "onNewIntent() with intent " + getIntent());
		super.onNewIntent(newIntent);
		
		setMapFromIntent(newIntent);
	}
	
	public void setMapFromIntent(Intent intent) {
		if (VERBOSE) Log.v(TAG, "Setting map using intent " + intent);
		
		// extract the location from the Intent
		int latitude = intent.getIntExtra(LocationViewerConstants.EXTRA_LATITUDE, DEFAULT_LATITUDE);
		int longitude = intent.getIntExtra(LocationViewerConstants.EXTRA_LONGITUDE, DEFAULT_LONGITUDE);
		if (DEBUG) Log.d(TAG, "setting map coordinates: (" + latitude + ", " + longitude + ")");
		GeoPoint location = new GeoPoint(latitude, longitude);
		
		// clear the previous location (if any)
		mItemizedOverlay.clearOverlayItems();
		
		// add location to the map overlay
		OverlayItem overlayItem = new OverlayItem(location, "", "");
		mItemizedOverlay.addOverlayItem(overlayItem);
		
		// adjust map centre and zoom
		MapController controller = mMapView.getController();
		controller.setCenter(location);
		controller.setZoom(DEFAULT_ZOOM);
		
		// redraw the map
		mMapView.invalidate();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}